package com.example.studentmanager.Service.impl;

import com.example.studentmanager.Dao.BaseDao;
import com.example.studentmanager.Dao.StudentDao;
import com.example.studentmanager.Pojo.Student;
import com.example.studentmanager.Service.StudentService;
import com.example.studentmanager.Util.Dbtool;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Studentimpl implements StudentService{
    @Override
    public List<Student> queryALL() throws SQLException {
        StudentDao stu=new StudentDao();
        List<Student> list;
        try (ResultSet rs = stu.queryallStudent()) {
            list = new ArrayList<>();
            while (rs.next()) {
                Student s = new Student();
                s.setStudentid(rs.getInt("studentid"));
                s.setCls(rs.getString("cls"));
                s.setName(rs.getString("name"));
                s.setHome(rs.getString("home"));
                s.setGrade(rs.getString("grade"));
                s.setPhone(rs.getInt("phone"));
                s.setState(rs.getString("state"));
                System.out.println(s.getName());
                list.add(s);
            }
        }
        return list;
    }

    @Override
    public List<Student> queryByKey(String key) {
        StudentDao stu=new StudentDao();

        BaseDao bs=new BaseDao();

        String sql=stu.QueryByKey();

        List<Student> list=new ArrayList<>();
        //缩减了也可以拿出来
        try( ResultSet rs = bs.SelectByKey(sql, key)){
            while (rs.next()){

                Student s = new Student();
                s.setStudentid(rs.getInt("studentid"));
                s.setCls(rs.getString("cls"));
                s.setName(rs.getString("name"));
                s.setHome(rs.getString("home"));
                s.setGrade(rs.getString("grade"));
                s.setPhone(rs.getInt("phone"));
                s.setState(rs.getString("state"));
                System.out.println(s.getName());
                list.add(s);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

       return list;
        //这里返回list 之后  控制器发给jsp 是的  这种javaee只能这么凑合写了   业务层进行具体操作  关闭就应该在这里了

    }

    @Override
    public int Update(Student student) {
        //Dao 对象
        StudentDao s=new StudentDao();
        //使用sql
       String sql= s.Updatestu();
       //baseDao
       BaseDao bs=new BaseDao();
       //参数1 sql   内容
       int r= bs.UpdateStudent(sql,student);

        return r;
    }

    @Override
    public int InsertIntoStudent(Student student) {
        StudentDao s=new StudentDao();
        String sql=s.InsertIntoStudent();
        BaseDao bs=new BaseDao();
     int r=   bs.InsertStudent(sql,student);

        return r;
    }





}
